﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="FileTablePage.xaml.cs" company="Microsoft">
//   Microsoft
// </copyright>
// <summary>
//   Defines the FileTablePage type.
// </summary>
// Copyright (c) Microsoft Corporation. All rights reserved.
// 
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
// EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
// OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
// ----------------------------------------------------------------------------------
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
// --------------------------------------------------------------------------------------------------------------------

namespace MySemanticSearch
{
    using System;
    using System.Windows.Controls;
    using System.Windows.Input;

    using MySemanticSearch.ViewModel;

    /// <summary>
    /// SL Dialog to create a new Filetable
    /// </summary>
    public partial class FileTablePage
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="FileTablePage"/> class.
        /// </summary>
        /// <param name="sqlInstance">
        /// The sql Instance.
        /// </param>
        public FileTablePage(string sqlInstance)
        {
            // Required to initialize variables
            InitializeComponent();
            this.MainGrid.KeyDown += this.UpdateBindings;
            this.DataContext = new FileTablePageViewModel(this) { SelectedSqlInstance = sqlInstance };
        }

        /// <summary>
        /// Updates bindings when the user hits an specific key
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The event arguments
        /// </param>
        private void UpdateBindings(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                var fileTableNameBinding = this.FtNameTextBox.GetBindingExpression(TextBox.TextProperty);
                if (fileTableNameBinding != null)
                {
                    fileTableNameBinding.UpdateSource();
                }

                var databaseNameBinding = this.DbNameTextBox.GetBindingExpression(TextBox.TextProperty);
                if (databaseNameBinding != null)
                {
                    databaseNameBinding.UpdateSource();
                }

                ((FileTablePageViewModel)this.DataContext).CreateFileTable();
            }
            
            if (e.Key == Key.Escape)
            {
                this.Close();
            }
        }
    }
}